package com.pfzy.ui.activity.pick

import android.os.Bundle
import android.text.format.DateFormat
import android.view.View
import android.widget.*
import androidx.fragment.app.FragmentActivity
import com.pfzy.ui.activity.pick.bean.CardBean
import com.pfzy.ui.activity.pick.bean.ProvinceBean
import com.pfzy.ui.databinding.ActivityPickerBinding
import com.pfzy.ui.log.MyLog
import com.pfzy.ui.view.picker.CustomDate2Picker
import java.util.*

class PickerActivity : FragmentActivity() {
    private lateinit var mActivity: FragmentActivity
    private lateinit var mBind: ActivityPickerBinding
    private var mPickerDlg1: PickerDlg? = null
    private var mPickerDlg2: PickerDlg2? = null
    private var mPickerDlg3: PickerDlg3? = null
    private var mPickerDlg4: PickerDlg4<Any>? = null
    private var mPickerDlg5: PickerDlg5<Any>? = null
    private var mPickerDlg6: PickerDlg6<Any>? = null
    private var mPickerDlg7: PickerDlg7<Any>? = null
    private var mPickerDlg8: PickerDlg8? = null
    private var mPickerDlg9: PickerDlg9? = null
    private var mPickerDlg10: PickerDlg10? = null

    private var date2Picker: CustomDate2Picker? = null

    private val options1Items: MutableList<ProvinceBean> = mutableListOf()
    private val options2Items: MutableList<MutableList<String>> = mutableListOf()
    private val cardItem: java.util.ArrayList<CardBean> = java.util.ArrayList<CardBean>()
    private val food = java.util.ArrayList<String>()
    private val clothes = java.util.ArrayList<String>()
    private val computer = java.util.ArrayList<String>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mActivity = this
        mBind = ActivityPickerBinding.inflate(layoutInflater)
        setContentView(mBind.root)
        initView()
    }

    private fun initView() {
        initOptionPicker()
        initCustomOptionPicker()
        initNoLinkOptionsPicker()
        pickerInit2()
    }

    /**
     * 条件选择器初始化
     */
    private fun initOptionPicker() {
        // 选项1
        options1Items.add(ProvinceBean(0, "广东", "描述部分", "其他数据"))
        options1Items.add(ProvinceBean(1, "湖南", "描述部分", "其他数据"))
        options1Items.add(ProvinceBean(2, "广西", "描述部分", "其他数据"))

        // 选项2
        val options2Items_01 = java.util.ArrayList<String>()
        options2Items_01.add("广州")
        options2Items_01.add("佛山")
        options2Items_01.add("东莞")
        options2Items_01.add("珠海")
        val options2Items_02 = java.util.ArrayList<String>()
        options2Items_02.add("长沙")
        options2Items_02.add("岳阳")
        options2Items_02.add("株洲")
        options2Items_02.add("衡阳")
        val options2Items_03 = java.util.ArrayList<String>()
        options2Items_03.add("桂林")
        options2Items_03.add("玉林")
        options2Items.add(options2Items_01)
        options2Items.add(options2Items_02)
        options2Items.add(options2Items_03)
    }

    /**
     * 条件选择器初始化，自定义布局
     */
    private fun initCustomOptionPicker() {
        for (i in 0..4) {
            cardItem.add(CardBean(i, "No.ABC12345 $i"))
        }
        for (i in cardItem.indices) {
            if (cardItem.get(i).getCardNo().length > 6) {
                val str_item: String = cardItem.get(i).getCardNo().substring(0, 6) + "..."
                cardItem.get(i).setCardNo(str_item)
            }
        }
    }

    /**
     * 不联动的多级选项
     */
    private fun initNoLinkOptionsPicker() {
        food.add("KFC")
        food.add("MacDonald")
        food.add("Pizza hut")
        clothes.add("Nike")
        clothes.add("Adidas")
        clothes.add("Armani")
        computer.add("ASUS")
        computer.add("Lenovo")
        computer.add("Apple")
        computer.add("HP")
    }

    private fun pickerInit2() {
        date2Picker = CustomDate2Picker(this)
        date2Picker!!.setHandler(object : CustomDate2Picker.ResultHandler {
            override fun handle(text: String, time: Long) {
                var retnTime = DateFormat.format("yyyy年MM月dd日 HH时mm分ss秒", time)
                _logd("选择时间返回 $text,$retnTime")
            }
        })
    }

    fun onPicker12(view: View) {
        val curCalendar = Calendar.getInstance()
        curCalendar.set(Calendar.SECOND, 0)
        curCalendar.set(Calendar.MILLISECOND, 0)
        date2Picker!!.show2(curCalendar.timeInMillis)
    }

    fun onPicker11(view: View) {}

    fun onPicker10(view: View) {
        if (mPickerDlg10 == null) {
            mPickerDlg10 = PickerDlg10(this)
            mPickerDlg10!!.setOnItemClickListener { day, hour, minutes ->
                _logd("选择时间 ${day!!.text} ${hour}:${minutes}")
            }
        }
        mPickerDlg10!!.showDialog()
    }

    fun onPicker9(view: View) {
        if (mPickerDlg9 == null) {
            mPickerDlg9 = PickerDlg9(this)
            mPickerDlg9!!.setOnItemClickListener { startHour, startMinutes, endHour, endMinutes ->
                _logd("选择时间 ${startHour}:${startMinutes}——${endHour}:${endMinutes}")
            }
        }
        mPickerDlg9!!.showDialog()
    }

    fun onPicker8(view: View) {
        if (mPickerDlg8 == null) {
            mPickerDlg8 = PickerDlg8(this)
        }
        mPickerDlg8!!.showDialog()
    }

    fun onPicker7(view: View) {
        if (mPickerDlg7 == null) {
            mPickerDlg7 = PickerDlg7(this)
            mPickerDlg7!!.setOnOptionsSelectChangeListener { options1, options2, options3 ->
                val str = "options1: $options1 ，options2: $options2，options3: $options3"
                Toast.makeText(mActivity, str, Toast.LENGTH_SHORT).show()
            }
            mPickerDlg7!!.setSelectOptions(0, 1, 1)
            mPickerDlg7!!.setNPicker(
                food as List<Any>?,
                clothes as List<Any>?,
                computer as List<Any>?
            )
        }
        mPickerDlg7!!.showDialog()
    }

    fun onPicker6(view: View) {
        if (mPickerDlg6 == null) {
            mPickerDlg6 = PickerDlg6(this)
            mPickerDlg6!!.setOnOptionsSelectChangeListener { options1, options2, options3 ->
                val str = "options1: $options1 ，options2: $options2，options3: $options3"
                Toast.makeText(mActivity, str, Toast.LENGTH_SHORT).show()
            }
            mPickerDlg6!!.setPicker(this.cardItem as List<Any>?) //添加数据
        }
        mPickerDlg6!!.showDialog()
    }

    fun onPicker5(view: View) {
        if (mPickerDlg5 == null) {
            mPickerDlg5 = PickerDlg5(this)
            mPickerDlg5!!.setOptionsSelectChangeListener { options1, options2, options3 ->
                val str = "options1: $options1 ，options2: $options2，options3: $options3"
                Toast.makeText(mActivity, str, Toast.LENGTH_SHORT).show()
            }
            mPickerDlg5!!.setSelectOptions(0, 1) //默认选中项
            mPickerDlg5!!.setPicker(
                options1Items as List<Any>?,
                options2Items as List<MutableList<Any>>?
            )
        }
        mPickerDlg5!!.showDialog()
    }

    fun onPicker4(view: View) {
        if (mPickerDlg4 == null) {
            mPickerDlg4 = PickerDlg4(this)
            mPickerDlg4!!.setOptionsSelectChangeListener { options1, options2, options3 ->
                val str = "options1: $options1 ，options2: $options2，options3: $options3"
                Toast.makeText(mActivity, str, Toast.LENGTH_SHORT).show()
            }
            mPickerDlg4!!.setSelectOptions(0, 1) //默认选中项
            mPickerDlg4!!.setPicker(
                options1Items as List<Any>?,
                options2Items as List<MutableList<Any>>?
            )
        }
        mPickerDlg4!!.showDialog()
    }

    fun onPicker3(view: View) {
        if (mPickerDlg3 == null) {
            mPickerDlg3 = PickerDlg3(this)
        }
        mPickerDlg3!!.showDialog()
    }

    fun onPicker2(view: View) {
        if (mPickerDlg2 == null) {
            mPickerDlg2 = PickerDlg2(this)
        }
        mPickerDlg2!!.showDialog()
    }

    fun onPicker1(view: View) {
        if (mPickerDlg1 == null) {
            mPickerDlg1 = PickerDlg(this)
        }
        mPickerDlg1!!.show();
    }

    private fun _logd(msg: String) {
        MyLog.logd("选择器: $msg")
    }
}